package Colorful;

/**
 * @author zihao
 * @date 2025/3/19 21:41
 * @VERSION
 * @description
 **/
import java.util.Scanner;

public class FlowerMgr {
    private Flower[] flowers; // 存储鲜花订单的数组
    private int count; // 当前鲜花订单的数量

    // 构造方法
    public FlowerMgr(int size) {
        flowers = new Flower[size];
        count = 0;
    }

    // 初始化鲜花订单
    public void initial() {
        // 添加一些初始数据
        flowers[0] = new Flower(1, "玫瑰", 5.0, 10);
        flowers[1] = new Flower(2, "百合", 8.0, 5);
        flowers[2] = new Flower(3, "郁金香", 4.5, 8);
        count = 3;
        System.out.println("鲜花管理系统初始化成功！");
    }

    // 主菜单
    public void mainMenu() {
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;

        while (loop) {
            System.out.println("\n===== 七彩鲜花销售系统 =====");
            System.out.println("1. 查询订单");
            System.out.println("2. 修改订单");
            System.out.println("3. 删除订单");
            System.out.println("4. 新增订单");
            System.out.println("0. 退出系统");
            System.out.print("请输入您的选择：");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除缓冲区

            switch (choice) {
                case 1:
                    searchAll();
                    break;
                case 2:
                    updateSale();
                    break;
                case 3:
                    deleteSale();
                    break;
                case 4:
                    addSale(scanner);
                    break;
                case 0:
                    System.out.println("退出系统，感谢使用！");
                    loop = false;
                    break;
                default:
                    System.out.println("输入有误，请重新输入！");
            }
        }
        scanner.close();
    }

    // 查询所有订单
    private void searchAll() {
        if (count == 0) {
            System.out.println("目前没有订单信息！");
            return;
        }

        System.out.println("\n===== 所有订单信息 =====");
        for (int i = 0; i < count; i++) {
            System.out.println(flowers[i].toString());
        }
    }

    // 修改订单
    private void updateSale() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要修改的鲜花编号：");
        int id = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区

        int index = findIndexById(id);
        if (index == -1) {
            System.out.println("未找到该鲜花订单！");
            return;
        }

        System.out.println("当前鲜花信息：" + flowers[index].toString());
        System.out.print("请输入新的鲜花名称（留空则不修改）：");
        String name = scanner.nextLine();
        if (!name.isEmpty()) {
            flowers[index].setName(name);
        }

        System.out.print("请输入新的鲜花价格（留空则不修改）：");
        String priceStr = scanner.nextLine();
        if (!priceStr.isEmpty()) {
            double price = Double.parseDouble(priceStr);
            if (price >= 0) {
                flowers[index].setPrice(price);
            } else {
                System.out.println("价格不能为负数，已忽略！");
            }
        }

        System.out.print("请输入新的销售数量（留空则不修改）：");
        String salesStr = scanner.nextLine();
        if (!salesStr.isEmpty()) {
            int sales = Integer.parseInt(salesStr);
            if (sales >= 0) {
                flowers[index].setSales(sales);
            } else {
                System.out.println("销售数量不能为负数，已忽略！");
            }
        }

        System.out.println("修改成功！修改后的鲜花信息：" + flowers[index].toString());
    }

    // 删除订单
    private void deleteSale() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要删除的鲜花编号：");
        int id = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区

        int index = findIndexById(id);
        if (index == -1) {
            System.out.println("未找到该鲜花订单！");
            return;
        }

        System.out.println("找到的鲜花信息：" + flowers[index].toString());
        System.out.print("确定要删除吗？(yes/no)：");
        String confirm = scanner.nextLine();

        if (confirm.equalsIgnoreCase("yes")) {
            // 删除操作：将该位置的元素移到数组末尾，然后减少计数
            for (int i = index; i < count - 1; i++) {
                flowers[i] = flowers[i + 1];
            }
            count--;
            System.out.println("删除成功！");
            searchAll(); // 显示最新的订单信息
        } else {
            System.out.println("已取消删除操作！");
        }
    }

    // 新增订单
    private void addSale(Scanner scanner) {
        if (count >= flowers.length) {
            System.out.println("订单已满，无法新增！");
            return;
        }

        System.out.println("===== 新增鲜花订单 =====");
        System.out.print("请输入鲜花编号：");
        int id = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区

        if (findIndexById(id) != -1) {
            System.out.println("该鲜花编号已存在，无法重复添加！");
            return;
        }

        System.out.print("请输入鲜花名称：");
        String name = scanner.nextLine();
        System.out.print("请输入鲜花价格：");
        double price = scanner.nextDouble();
        scanner.nextLine(); // 清除缓冲区
        System.out.print("请输入销售数量：");
        int sales = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区

        if (price < 0) {
            System.out.println("价格不能为负数，已设置为0！");
            price = 0;
        }

        if (sales < 0) {
            System.out.println("销售数量不能为负数，已设置为0！");
            sales = 0;
        }

        flowers[count] = new Flower(id, name, price, sales);
        count++;
        System.out.println("新增成功！新增的鲜花信息：" + flowers[count - 1].toString());
    }

    // 根据鲜花编号查找索引
    private int findIndexById(int id) {
        for (int i = 0; i < count; i++) {
            if (flowers[i].getId() == id) {
                return i;
            }
        }
        return -1;
    }
}